package com.xu.crawler.core;

import com.xu.crawler.dao.HkStockDao;
import com.xu.crawler.dao.entity.HkStockDO;
import com.xu.crawler.listener.DownloadFailListener;
import com.xu.crawler.pipeline.MongoPipeline;
import com.xu.crawler.processor.HkStockListPageProcessor;
import com.xu.crawler.processor.StockHolderPageProcessor;
import com.xu.crawler.service.FailRecordService;
import org.assertj.core.util.Lists;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务
 * <p>
 * Created by xuguangwu486
 */
@Component
public class ScheduleTasks {

    @Resource
    private FailRecordService failRecordService;

    @Resource
    private HkStockDao hkStockDao;

    //    @Scheduled(cron = "30 41 23 * * ?")
//    @Scheduled(cron = "0 */1 * * * ?")
    private void scmpData() {
//        Spider spider = Spider.create(new ScmpPageProcessor());
//        spider.addPipeline(new MongoPipeline());
//        spider.setSpiderListeners(Lists.newArrayList(new DownloadFailListener()));
//        spider.addUrl("https://www.scmp.com/sitemap_news.xml").thread(50).run();
//        spider.addUrl("https://www.scmp.com/sitemap_business.xml").thread(50).run();

//        MySpider spider = MySpider.create(new ScmpPageProcessor());
//        spider.addPipeline(new MongoPipeline());
//        spider.setSpiderListeners(Lists.newArrayList(new DownloadFailListener()));
//        spider.addUrl("https://www.scmp.com/sitemap_news.xml").thread(50).run();
//        spider.addUrl("https://www.scmp.com/sitemap_business.xml").thread(50).run();
    }

    @Scheduled(cron = "00 29 10 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    private void stockHolder() {
        Spider spider = Spider.create(new StockHolderPageProcessor());
        spider.addPipeline(new MongoPipeline());
        List<HkStockDO> hkStockDOList = hkStockDao.queryAll();
//        hkStockDOList.clear();
        hkStockDOList.forEach(hkStockDO -> {
            try {
                spider.addUrl("http://stockpage.10jqka.com.cn/" + hkStockDO.getCode() + "/holder/").run();
//                spider.addUrl("http://stockpage.10jqka.com.cn/HK0001/holder/").run();
                Thread.sleep(3000);
            } catch (Exception e) {

            }
        });
//        spider.addUrl("http://stockpage.10jqka.com.cn/HK0001/holder/").run();

    }

    /**
     * 获取港股列表
     */
//    @Scheduled(cron = "0/5 * * * * ?")
    private void hkStockList() {
        if (hkStockDao.count() == 0) {
            Spider spider = Spider.create(new HkStockListPageProcessor());
            spider.addPipeline(new MongoPipeline());
            spider.addUrl("http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?cb=jQuery112408528879082570053_1534681138163&type=CT&token=4f1862fc3b5e77c150a2b985b12db0fd&sty=CTF&js=(%7Bdata%3A%5B(x)%5D%2CrecordsFiltered%3A(tot)%7D)&cmd=C._HKS&st=(ChangePercent)&sr=-1&p=2&ps=3000&_=1534681138166");
            spider.run();
        }
    }

}
